package com.xiaohui.weibo.controller;

import com.xiaohui.weibo.dao.WeiBoDao;
import com.xiaohui.weibo.entity.User;
import com.xiaohui.weibo.entity.WeiBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.Date;
import java.util.List;

@Controller
public class WeiBoController {
    @Autowired
    private WeiBoDao weiBoDao;

    @GetMapping("/index")
    public String index(Model model){
        List<WeiBo> list=weiBoDao.findAll();
        model.addAttribute("weibos",list);
        return "list";
    }

    @GetMapping("/weibo/add")
    public String add(){
        return "add";
    }

    @PostMapping("/weibo/add")
    public String add(WeiBo weiBo, @SessionAttribute("cur_user") User user){
        weiBo.setCreateTime(new Date());
        weiBo.setReadCount(0);
        weiBo.setImg("c1.jpg");
        weiBo.setUserId(user.getId());
        weiBoDao.save(weiBo);
        return "redirect:/index";
    }
}
